﻿using AutoMapper;
using Mes.Domain.BoundedContext;
using System.Linq.Expressions;
using Microsoft.EntityFrameworkCore;
using Mes.Infrastructure.Data.BoundedContext.Extensions;
using Mes.Infrastructure.Data.BoundedContext.RepositoryBase;
using Mes.Application.Common;
using Mes.Application.Common.Resource;
using Mes.Application.DTO.Inputs.MasterData;
using Mes.Application.DTO.Results.MasterData;
using Mes.Domain.BoundedContext.Entities.MasterData;
using Mes.Infrastructure.Data.BoundedContext.Repositories.MasterData;

namespace Mes.Application.Services.MasterData
{
    public class ProductionUnitTypeService : IProductionUnitTypeService
    {
        private readonly IRepository<ProductionUnitType> _productionUnitTypeRepository;
        private readonly IProductionUnitTypeRepository _productionUnitTypeRepositoryInterface;
        private readonly IMapper _mapper;
        public ProductionUnitTypeService(IRepository<ProductionUnitType> productionUnitTypeRepository, IMapper mapper, IProductionUnitTypeRepository productionUnitTypeRepositoryInterface)
        {
            if (productionUnitTypeRepository == null || mapper == null || productionUnitTypeRepositoryInterface == null)
            {
                throw new ArgumentNullException();
            }
            _productionUnitTypeRepository = productionUnitTypeRepository; //注入仓储类
            _mapper = mapper;
            _productionUnitTypeRepositoryInterface = productionUnitTypeRepositoryInterface;
        }

        /// <summary>
        /// 新增生产单元类型
        /// </summary>
        /// <param name="productionUnitTypeInput"></param>
        /// <returns></returns>
        /// <exception cref="CustomException"></exception>
        public async Task<ProductionUnitType> AddProductionUnitTypeAsync(ProductionUnitTypeInput productionUnitTypeInput)
        {
            if (await _productionUnitTypeRepository.ExistsAsync(x => x.Description == productionUnitTypeInput.Description))
            {
                throw new CustomException(Messages.ProductionUnitTypeExist);
            }

            var productionUnitType = _mapper.Map<ProductionUnitType>(productionUnitTypeInput);
            await _productionUnitTypeRepository.AddAsync(productionUnitType);
            int status = await _productionUnitTypeRepository.SaveAsync();
            if (status <= 0)
            {
                throw new CustomException(Messages.SavingFailure);
            }
            return productionUnitType;
        }

        /// <summary>
        /// 修改生产单元类型
        /// </summary>
        /// <param name="productionUnitTypeUpdateInput"></param>
        /// <returns></returns>
        /// <exception cref="CustomException"></exception>
        public async Task UpdateProductionUnitTypeAsync(ProductionUnitTypeUpdateInput productionUnitTypeUpdateInput)
        {
            var productionUnitType = _productionUnitTypeRepository.Find(x => x.Id == productionUnitTypeUpdateInput.Id);
            if (productionUnitType == null)
            {
                throw new CustomException(Messages.ProductionUnitTypeNotFound);
            }
            productionUnitType.UpdateTime = DateTime.Now;
            productionUnitType.Type = productionUnitTypeUpdateInput.Type;
            productionUnitType.Description = productionUnitTypeUpdateInput.Description;
            productionUnitType.Note = productionUnitTypeUpdateInput.Note;
            _productionUnitTypeRepository.Update(productionUnitType);
            int status = await _productionUnitTypeRepository.SaveAsync();
            if (status <= 0)
            {
                throw new CustomException(Messages.SavingFailure);
            }
        }

        /// <summary>
        /// 删除生产单元类型
        /// </summary>
        /// <param name="ids"></param>
        /// <returns></returns>
        /// <exception cref="CustomException"></exception>
        public async Task DeleteProductionUnitTypeAsync(int[] ids)
        {
            Expression<Func<ProductionUnitType, bool>> filter = x => ids.Contains(x.Id);
            if (filter == null)
            {
                throw new CustomException(Messages.ProductionUnitTypeNotFound);
            }
            await _productionUnitTypeRepository.DeleteAsync(filter);
            int status = await _productionUnitTypeRepository.SaveAsync();
            if (status <= 0)
            {
                throw new CustomException(Messages.SavingFailure);
            }
        }

        /// <summary>
        /// 根据主键编号查询生产单元类型
        /// </summary>
        /// <param name="id"></param>
        /// <returns>ProductionUnitTypeResult</returns>
        /// <exception cref="CustomException"></exception>
        public async Task<ProductionUnitTypeResult> GetProductionUnitTypeAsync(int id)
        {
            var productionUnitType = await _productionUnitTypeRepository.Query().Where(x => x.Id == id).FirstOrDefaultAsync();
            if (productionUnitType == null)
            {
                throw new CustomException(Messages.ProductionUnitTypeNotFound);
            }
            return _mapper.Map<ProductionUnitTypeResult>(productionUnitType);
        }

        /// <summary>
        /// 分页查询生产单元类型
        /// </summary>
        /// <param name="productionUnitTypePageInput"></param>
        /// <returns>PagedItemsData<ProductionUnitTypeResult></returns>
        /// <exception cref="CustomException"></exception>
        public async Task<PagedItemsData<ProductionUnitTypeResult>> GetProductionUnitTypesAsync(ProductionUnitTypePageInput productionUnitTypePageInput)
        {
            var productionUnitType = await _productionUnitTypeRepository.Query().Where(x => x.Type.Contains(productionUnitTypePageInput.Type)).ToPagedAsync(productionUnitTypePageInput.PagedInput);
            if (productionUnitType == null)
            {
                throw new CustomException(Messages.ProductionUnitTypeNotFound);
            }
            return _mapper.Map<PagedItemsData<ProductionUnitTypeResult>>(productionUnitType);
        }
    }
}
